<template>
	<view>
		<!-- 搜索区域 -->
		<view class="home-search">
			<my-search @click="gotoSearch"></my-search>
		</view>
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :circular="true" :interval="1500" :duration="1000">
			<swiper-item  v-for="(item,index) in swiperList" :key="item.goods_id">
				<view class="swiper-item">
					<image :src="item.image_src" ></image>
				</view>
			</swiper-item>
		</swiper>
		<!-- 分类导航区域 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item,index) in navList" :key="index" @click="navClickHandle(item)">
				<image class="nav-img" :src="item.image_src"></image>
			</view>
		</view>
		<!-- 楼层区域 -->
		<!-- 楼层的容器 -->
		<view class="floor-list">
			<!-- 楼层里面的每一项 -->
			<view class="floor-item" v-for="(item,index) in floorList" :key="index">
				<!-- 楼层里面的标题 -->
					<image class="floor-title" :src="item.floor_title.image_src" ></image>
			<!-- 楼层里面的图片区域 -->
			<view class="floor-img-box">
				<!-- 左侧大盒子 -->
				<navigator :url="item.product_list[0].url" class="floor-img-left">
					<image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width +'rpx'}" mode="widthFix"></image>
				</navigator>
				<!-- 右侧四个小盒子 -->
				<view class="floor-img-right">
					<navigator :url="item.url" class="floor-img-right-item" v-for="(item,index) in item.product_list" :key="index">
						<template v-if="index !== 0">
						<image :src="item.image_src" :style="{width:item.image_width+'rpx'}" mode="widthFix"></image>
						</template>
					</navigator>
				</view>
			</view>
				
			</view>
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 轮播图的数据列表
				swiperList:[],
				// 获取分类导航栏数据
				navList:[],
				// 定义楼层数据
				floorList:[]
			};
		},
		onLoad() {
			// 调用方法  获取轮播图的数据
			this.getSwiperList()
			// 调用方法  获取分类导航栏的数据
			this.getNavList()
			// 获取楼层数据的方法
			this.getFloorList()
		},
		methods:{
			   // 获取轮播图数据  
			async getSwiperList(){
				const {data:res} = await uni.$http.get('/api/public/v1/home/swiperdata')
				console.log(res)
				if(res.meta.status!== 200) return	 uni.$showMsg()
				this.swiperList = res.message
				// uni.$showMsg('数据请求成功')
			},
			// 分类导航的数据
		   async getNavList(){
			const {data:res} = await uni.$http.get('/api/public/v1/home/catitems')
			if(res.meta.status !== 200) return uni.$showMsg()
			this.navList = res.message
			},
			
			// 获取name 点击分类图标进行页面跳转
			navClickHandle(item){
				if(item.name === '分类'){
					uni.switchTab({
						url:'/pages/cate/cate'
					})
				}
				
			},
			
			// 获取楼层数据
		async getFloorList(){
		    const {data:res} = await uni.$http.get('/api/public/v1/home/floordata')
			if(res.meta.status !== 200) return uni.$showMsg()
			// 双层遍历，设置一个 url指向 subpack 后面跟参数
			console.log(res)
			res.message.forEach(floor=>{
				floor.product_list.forEach(prod=>{
					prod.url = '/subpkg/goods_list/goods_list?'+prod.navigator_url.split('?')[1]
				})
			})
			this.floorList = res.message
			},
			gotoSearch(){
				uni.navigateTo({
					url:'/subpkg/my-search/my-search'
				})
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 330rpx;
		.swiper-item,image{
			width: 100%;
			height: 100%;
			
		}
	}
	
	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 20rpx 0;
		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}
	
	.floor-title {
		width: 100%;
		height: 60rpx;
	}
	
	.floor-img-box {
		display: flex;
		padding-left: 10rpx;
	}
	
	.floor-img-right {
		display: flex;
		justify-content: space-around;
		flex-wrap: wrap;
	}
	.home-search {
		position: sticky;
		top: 0;
		z-index: 9999;
	}

</style>
